
var inputele=document.querySelector(".input-tip");

var btn1 = document.querySelector(".tab-email");
var btn2 = document.querySelector(".tab-phone");

var btn3 = document.querySelector(".email-login");
var btn4 = document.querySelector(".phone-login");

var btn5 = document.querySelector(".pwdlogin");
var btn6 = document.querySelector(".codelogin");
//账号密码登录
btn1.onclick = function () {
    inputele.innerText="";
    btn3.style.display="block";
    btn5.style.display="block";
    btn4.style.display="none";
    btn6.style.display="none";
    $(btn1).removeClass("tab-email");
    $(btn1).addClass("tab-phone");
    $(btn2).removeClass("tab-phone");
    $(btn2).addClass("tab-email");
}

//验证码登录
btn2.onclick = function () {
    inputele.innerText="";
    btn3.style.display="none";
    btn5.style.display="none";
    btn4.style.display="block";
    btn6.style.display="block";
    $(btn2).removeClass("tab-email");
    $(btn2).addClass("tab-phone");
    $(btn1).removeClass("tab-phone");
    $(btn1).addClass("tab-email");
}

document.querySelector(".form-control").onblur = checkInputPhone;
document.querySelector(".text-control").onblur = checkInputCode;

//检查输入的手机号
var telnoTip;
function checkInputPhone() {
    var telno = document.querySelector(".form-control").value;
    if (telno==null||telno===""){
        telnoTip="电话号码不能为空";
    }else if (telno.search(/^1[3-578]\d{9}$/)===-1){
        telnoTip="电话号码格式不正确";
    }else{
        telnoTip="";
    }
    var inputele=document.querySelector(".input-tip");
    inputele.innerText=telnoTip;
}

//检查输入的验证码
var codeTip;
function checkInputCode() {
    var code = document.querySelector(".text-control").value;

    if (code==null||code===""){
        codeTip="验证码不能为空";
    }else if (code.search(/^[a-zA-Z0-9]{6}$/)===-1){
        codeTip="验证码格式不正确"
    }else {
        codeTip="";
    }
    var inputele=document.querySelector(".input-tip");
    inputele.innerText=codeTip;
}




document.querySelector(".input-email").onblur = checkInputPwd;
document.querySelector(".custPwd").onblur = checkInputPwd;

// 检查输入的账号 密码
var pwdTip;
function checkInputPwd() {
    var custNo = document.querySelector(".input-email").value;
    var custPwd = document.querySelector(".custPwd").value;
    if (custNo==null||custNo===""){
        pwdTip="账号不能为空";
    }else if (custNo.search(/^1[3-578]\d{9}$/)===-1){
        pwdTip="账号格式不正确";
    }else if (custPwd==null||custPwd===""){
        pwdTip="密码不能为空";
    }else if (custPwd.search(/^(?!\D+$)(?![^a-zA-Z]+$)\S{6,}$/)=== -1){
        pwdTip="密码格式不正确"
    }else {
        pwdTip="";
    }
    var inputele=document.querySelector(".input-tip");
    inputele.innerText=pwdTip;
}




/**
 * 日期转换 把倒计时总秒数转换为“0天0小时0分0秒”的格式
 * @param {*} seconds 总秒数
 * @returns String xx天xx小时xx分xx秒
 */
function dateTransform(seconds) {
    let [day, hour, minute, second] = [0, 0, 0, 0] // 初始化
    if (seconds > 0) {
        day = Math.floor(seconds / (60 * 60 * 24))
        hour = Math.floor(seconds / (60 * 60)) - day * 24
        minute = Math.floor(seconds / 60) - day * 24 * 60 - hour * 60
        second = Math.floor(seconds) - day * 24 * 60 * 60 - hour * 60 * 60 - minute * 60
    }
    // 小于10的，在前面加0
    day = day < 10 ? '0' + day : day
    hour = hour < 10 ? '0' + hour : hour
    minute = minute < 10 ? '0' + minute : minute
    second = second < 10 ? '0' + second : second
    return day + '天' + hour + '小时' + minute + '分' + second + '秒'
}

//发送验证码
var sms = "";
function getMsg() {
    var mobilePhone = $("#mobilePhone").val();
    console.log(mobilePhone)
    $.ajax({
        type: "post",  // 请求的类型  get  post
        cache:false,
        url: "../loginGetCode",
        contentType: "application/json",
        //  dataType: "json",
         data:`{"mobilePhone":${mobilePhone}}`,
        success: function(result){
             console.log("result:" + JSON.stringify(result));
            sms = result.data;
            console.log(sms);
        }
    });
}

//检查验证码
function checkCode() {
    var mobilePhone = $("#mobilePhone").val();
    var code = $("#msgCode").val();
    console.log(code)
    console.log(sms)
    if (code === "") {
        alert("请输入验证码");
    } else {
        if (sms === code) {
            $.ajax({
                type: "post",
                contentType: "application/json",
                data:
                    `{"mobilePhone":${mobilePhone}}`,
                // dataType: "json",
                url: "../codeLogin",
                success: function (result) {
                    console.log("result:" + result);
                    if (result.code === 200)
                        location.href = "http://localhost:8080/index.jsp";
                    else
                        alert(result.data);
                }

            })
        } else {
            // alert("验证码错误");
        }
    }
}

//检查账号密码
function checkPwd() {
    var number = $("#number").val();
    var numberPwd = $("#numberPwd").val();
    console.log(number)
    console.log(numberPwd)
    if (numberPwd === "") {
        alert("请输入密码");
    } else {
        $.ajax({
            type: "post",
            contentType: "application/json",
            data:
                `{"number":${number}}`
                    // `{"numberPwd":${numberPwd}}`
            ,
            // dataType: "json",
            url: "../pwdLogin",
            success: function (result) {
                console.log("result:" + result);
                var pwd = result.data;
                if (result.code === 200){
                    if (pwd===numberPwd){
                        location.href = "http://localhost:8080/index.jsp";
                    }else {
                        alert("账号或密码错误");
                    }
                }
                else
                    alert(result.data);
            }

        })
    }
}
/**
 * 倒计时
 * @param {*} type 类型 0-给定具体多少秒倒计时（默认）；1-给定目标结束时间戳，单位为秒，目标是转换为“xx秒后结束”的格式
 * @param {*} timestamp 当type==0时表示具体秒数，默认60；type==1时为目标结束时间戳，目标是转换为“xx天xx小时xx分xx秒”的格式
 */
let timestamp;
var phoneNum;
let seconds =0;
document.querySelector(".send-button").onclick=function countDown(timestamp = 60, type = 0) {
    phoneNum = document.querySelector(".form-control").value;
    checkInputPhone()
    if (telnoTip===""&&seconds===0) {
        getMsg()
         seconds = 60 // 倒计时总秒数
        if (type == 1) {
            let currentTimestamp = Math.round(new Date() / 1000) // 当前时间戳，单位秒
            console.log(currentTimestamp);
            seconds = timestamp - currentTimestamp
            console.log(seconds);
        }

        // 如果目标时间小于等于当前时间，不需要继续进行了
        if (seconds <= 0) return

        // 定时器
        let timer = setInterval(() => {
            seconds--
            let result = type == 0 ? seconds + "s后重发" : dateTransform(seconds)
            console.log(result)
            // 把转换后的结果显示出来
            document.getElementById('countDown').innerHTML = result

            if (seconds <= 0) {
                clearInterval(timer)
                document.getElementById('countDown').innerHTML = "发送验证码"
            }
        }, 1000)
    } else {
        alert("请输入手机号");
        return false;
    }
}

//验证码登录
document.querySelector(".codelogin-button").onclick=function () {
    checkInputPhone()
    checkInputCode()
    //判断验证码是否正确
    if (codeTip === ""&&telnoTip==="") {
        checkCode()
    }
}

//账号密码登录
document.querySelector(".pwdlogin-button").onclick=function () {
    checkInputPwd()
    //判断账号密码是否正确
    if (pwdTip===""){
        checkPwd()
    }

}




